//
//  AlbumItem.m
//  AlbumDemo
//
//  Created by hejinhui on 2018/1/22.
//  Copyright © 2018年 hejinhui. All rights reserved.
//

#import "AlbumItem.h"
#import "UIView+Extension.h"
#import "AlbumModel.h"
#import "UIImageView+WebCache.h"
#import "AlbumPhoto.h"
@interface AlbumItem()
//图片
@property(nonatomic,strong)UIImageView *photoImgV;
//按钮
@property(nonatomic,strong)UIButton *addBtn;
//标题
@property(nonatomic,strong)UILabel *titLabel;
//rect
@property(nonatomic,assign)CGRect rect;

@end

@implementation AlbumItem

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        //添加到view
        [self addSubview:self.photoImgV];
        [self addSubview:self.addBtn];
        [self addSubview:self.titLabel];
    }
    return self;
}

-(void)setItemFrame:(CGRect)itemFrame
{
    _rect = itemFrame;
    
}
-(void)addNewModel:(NSArray <AlbumModel *>*)arr
{
    
    
}
-(void)setModel:(AlbumModel *)model
{
    _model = model;
   
    //title
    self.titLabel.text = model.title;
    //btn hidden or no hidden
    self.addBtn.hidden = model.isHiddenBtn ? YES : NO;
    
    AlbumPhoto *photo = [model.photoArr lastObject];
    
    //没有任何图片显示默认图
    if ([photo.url isEqualToString:@""] && !photo.image) {
        
        self.photoImgV.image = [UIImage imageNamed:photo.placehoder];
    }
    //imageView
    
    if (photo.url && ![photo.url isEqualToString:@""]) {
        
        [self.photoImgV sd_setImageWithURL:[NSURL URLWithString:photo.url] placeholderImage:[UIImage imageNamed:@"IDCard_1"] options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            
            if (error) {
                NSLog(@"error----%@",error.localizedDescription);
            }
        }];
    }else
    {
        //        self.photoImgV.image = photo.image;

    }
   
}

//初始化
-(UIImageView *)photoImgV
{
    if (!_photoImgV) {
        _photoImgV = [[UIImageView alloc]init];
    }
    return _photoImgV;
}

-(UIButton *)addBtn
{
    if (!_addBtn) {
        
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.backgroundColor = [UIColor orangeColor];
        [_addBtn setImage:[UIImage imageNamed:@"ic_addphoto"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(UILabel *)titLabel
{
    if (!_titLabel) {
        _titLabel = [[UILabel alloc]init];
        _titLabel.textAlignment = NSTextAlignmentCenter;
        _titLabel.textColor = [UIColor darkTextColor];
        _titLabel.font = [UIFont systemFontOfSize:10];
    }
    return _titLabel;
}

-(void)btnClick:(UIButton *)sender
{
    if (self.itemBlock) {
        self.itemBlock(self);
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];

    //imageView的frame
    self.photoImgV.frame = CGRectMake(0, 0, _rect.size.width, _rect.size.height - 30);
    
   //title的frame
    self.titLabel.frame = CGRectMake(0, CGRectGetMaxY(self.photoImgV.frame), _rect.size.width - 30, 30);
    
   //addBtn的frame
    self.addBtn.frame = CGRectMake(_rect.size.width - 30, CGRectGetMinY(self.titLabel.frame), 25,25);
}


@end
